package com.szwistar.common.datastruct;


/**
 * hex文件中的一段连续数据
 * @author mirror
 *
 */
public class HexFileData {
	//'00' Data Record：用来记录数据，HEX文件的大部分记录都是数据记录
	public static final byte TYPE_DR = 0x00;
	//'01' End of File Record: 用来标识文件结束，放在文件的最后，标识HEX文件的结尾
	public static final byte TYPE_EOFR = 0x01;
	//'02' Extended Segment Address Record: 用来标识扩展段地址的记录
	public static final byte TYPE_ESAR = 0x02;
	//'03' Start Segment Address Record:开始段地址记录
	public static final byte TYPE_SSAR = 0x03;
	//'04' Extended Linear Address Record: 用来标识扩展线性地址的记录
	public static final byte TYPE_ELAR = 0x04;
	//'05' Start Linear Address Record:开始线性地址记录
	public static final byte TYPE_SLAR = 0x05;

	//当前数据帧位置
	private int framePos = 0;

	//起始地址
	private int addr;

	//数据类型
	private byte dataType;

	//数据
	private byte[] data;

	public void appendData(byte[] apdData, int start, int len) {
		byte[] newData = new byte[data.length+len];
		System.arraycopy(data, 0, newData, 0, data.length);
		System.arraycopy(apdData, start, newData, data.length, len);
		data = newData;
	}

	public int getAddr() {
		return addr;
	}

	public void setAddr(int addr) {
		this.addr = addr;
	}

	public byte getDataType() {
		return dataType;
	}

	public void setDataType(byte dataType) {
		this.dataType = dataType;
	}

	public byte[] getData() {
		return data;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	public void setData(byte[] data, int start, int len) {
		this.data = new byte[len];
		System.arraycopy(data, start, this.data, 0, len);
	}

	public int getNextAddr() {
		return (addr+data.length);
	}

	/**
	 * 获取一帧数据
	 * @param len
	 * @return
	 */
	public byte[] getFrame(int len) {
		if( framePos >= data.length ) {
			return null;
		}
		int relLen = framePos+len<data.length ? len : data.length-framePos;
		byte[] retData = new byte[relLen];
		System.arraycopy(data, framePos, retData, 0, retData.length);
		framePos += retData.length;
		return retData;
	}

	/**
	 * 将帧开始地址置为零
	 * */
	public void resetFramePos() {
		framePos = 0;
	}
}
